// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Provides a Command object responsible for executing a request to the server
 * to verify authentication and create an appropriate rb.auth.User object.
 */

goog.provide('rb.auth.AuthCheckCommand');

goog.require('rb.auth.AuthLevel');
goog.require('rb.auth.User');

goog.require('relief.rpc.Command');



/**
 * A Command object that encapsulates a request to the server for the user's
 * auth/auth details.
 *
 * @param {function(rb.auth.User)} onResponse Called with a user object when
 *    the request returns.  If the request fails, an anonymous user is returned.
 *
 * @constructor
 * @extends {relief.rpc.Command}
 */
rb.auth.AuthCheckCommand = function(onResponse) {
  goog.base(this, onResponse, onResponse, 'AuthCheckCommand:' + goog.now(),
            '/api/auth/check');
};
goog.inherits(rb.auth.AuthCheckCommand, relief.rpc.Command);


/**
 * Expects a server response with the following structure, based on the details
 * provided by App Engine's UserService.
 *
 * @typedef {{loggedIn: boolean, isAdmin: boolean,
 *            nickname: string, email: string, id: string}}
 * @private
 */
rb.auth.AuthCheckCommand.Response_;


/**
 * Method to be called by the RPC service on a successful request.
 *
 * @param {goog.net.XhrManager.Event} event The COMPLETE event for the XHR Req.
 * @override
 */
rb.auth.AuthCheckCommand.prototype.onSuccess = function(event) {
  var xhr = event.target,
      response = /** @type {rb.auth.AuthCheckCommand.Response_} */
      (xhr.getResponseJson());

  var authLevel = response['isAdmin'] ? rb.auth.AuthLevel.ADMIN :
                      response['loggedIn'] ? rb.auth.AuthLevel.NORMAL_USER :
                                             rb.auth.AuthLevel.ANONYMOUS,
      newUser = authLevel > rb.auth.AuthLevel.ANONYMOUS ?
                    response['newUser'] : false;

  var user = new rb.auth.User(response['nickname'], response['email'],
                              response['id'], authLevel, newUser);

  this.callersOnSuccess(user);
};


/**
 * Method to be called by the RPC service on a failed request.  Simply returns
 * an anonymous user to the client.
 *
 * @param {goog.net.XhrManager.Event} event The COMPLETE event for the XHR Req.
 * @override
 */
rb.auth.AuthCheckCommand.prototype.onFailure = function(event) {
  var user = new rb.auth.User('', '', '', rb.auth.AuthLevel.ANONYMOUS, false);
  this.callersOnSuccess(user);
};
